package cn.shangqizhenshuai.core;

import cn.shangqizhenshuai.convert.DbColumnType;
import cn.shangqizhenshuai.convert.MySqlTypeConvert;
import cn.shangqizhenshuai.entity.ColumnEntity;
import cn.shangqizhenshuai.entity.JavaEntity;
import cn.shangqizhenshuai.entity.TableEntity;
import cn.shangqizhenshuai.util.Util;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.apache.log4j.Logger;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.*;

/**
 * 实体类生成工具
 */
public class EntityExecute  extends ExecuteCore{
    public Logger log = Logger.getLogger(this.getClass());


    @Override
    public HashMap<String, List> execute(HashMap<String, List> context) throws IOException, TemplateException {
        ArrayList<JavaEntity> javaEntities = new ArrayList<JavaEntity>();

        //从上下文中获取表信息
        List<TableEntity> selecttabcolumn = context.get("tabinfo");
        //配置模板文件对象
        Configuration conf=new Configuration();
        //导入模板目录
        File file = new File("src/main/resources/");
        conf.setDirectoryForTemplateLoading(file);
        Template temp = conf.getTemplate(this.getFTLname());
        //开始文件输出
        String packagename = this.getPackagename();
        String[] split = packagename.split("\\.");
        String outpath = "src/main/java/"+this.getOutFilePath();
        for (String s : split) {
            outpath +="/"+s;
    }
        for (TableEntity tableEntity : selecttabcolumn) {
            //创建页面解析所需要的对象

            JavaEntity javaEntity = new JavaEntity();
            //设置是否启动spring配置
            javaEntity.setOpen_spring_annotate(this.isOpen_spring_annotate());
            javaEntity.setImPackage(packagename);
            Set<String> impotrs = new HashSet<String>();
            HashMap<String, HashMap<String, String>> column_name_and_type_map = new HashMap<String, HashMap<String, String>>();
            String javaname = Util.upperCase(Util.sub(tableEntity.getTabname()));
            javaEntity.setJavaname(javaname);

                javaEntity.setTableEntity(tableEntity);
                //处理映射类型
                List<ColumnEntity> columnEntitys = tableEntity.getColumnEntitys();
            //映射表名称和java 名称对应关系
            HashMap<String, String> hashMap = new HashMap<String, String>();
            HashMap<String, String> hashMap1 = new HashMap<String, String>();

            for (ColumnEntity columnEntity : columnEntitys) {
                    HashMap<String, String> stringStringHashMap = new HashMap<String, String>();
                    String data_type = columnEntity.getDATA_TYPE();
                        MySqlTypeConvert mySqlTypeConvert = new MySqlTypeConvert();
                        DbColumnType dbColumnType = mySqlTypeConvert.processTypeConvert(columnEntity.getDATA_TYPE());
                        //添加类型
                        stringStringHashMap.put(data_type,dbColumnType.getType());
                        String column_name = columnEntity.getCOLUMN_NAME();
                        String sname = Util.upperCase(Util.sub(column_name));
                        hashMap.put(sname,columnEntity.getCOLUMN_NAME());
                        hashMap1.put(columnEntity.getCOLUMN_NAME(),sname);
                        column_name_and_type_map.put(sname,stringStringHashMap);
                        //添加需要导入的包
                        if(!((dbColumnType.getPkg()==null)||dbColumnType.getPkg().equals(""))){
                        //存在包就添加
                            impotrs.add(dbColumnType.getPkg());
                        }

                }
            javaEntity.setJavaname_tabname_map(hashMap);
            javaEntity.setTabname_javaname_map(hashMap1);
                //处理表名称为
                String sub = Util.sub(tableEntity.getTabname());
                //处理后 表名大写
                String uptabname = Util.upperCase(sub);

                String tempout = outpath;
                File file1 = new File(tempout);
                file1.mkdirs();
                javaEntity.setColumnnameandtypemap(column_name_and_type_map);
                javaEntity.setImpotrs(impotrs);
                //开始循环输出文件
                javaEntities.add(javaEntity);
                Writer out=new FileWriter(new File(outpath+"/"+javaEntity.getJavaname()+".java"),false);
                temp.process(javaEntity, out);
                out.flush();
                out.close();

        }
        context.put("entityinfo",javaEntities);

        return context;
    }



}
